package game.event;

import java.util.ArrayList;
import java.util.List;

public class EventLog {
	private List<Event<?>> events = new ArrayList<Event<?>>();
	private boolean isNormalized = false;
	
	public void addEvent(Event<?> event){
		events.add(event);
	}
	
	public List<Event<?>> getEventsSince(int index){
		return events.subList(index, events.size());
	}
	
	/**
	 * Gets the event at the give index
	 * @param index of the event to return
	 * @return the event requested
	 * @throws IndexOutOfBoundsException if the index is invalid
	 */
	public Event<?> getEvent(int index){
		return events.get(index);
	}
	
	/**
	 * Normalizes the event log as a set of durations.
	 * Hopefully will make playback of events simpler.
	 * The last event is not normalized since it has no
	 * duration and playback should simply stop on the last
	 * event.
	 */
	public void normalizeLog(){
		if(!isNormalized){
			for (int i = 0; i < events.size() - 1; i++)
				events.get(i).normalizeTS(events.get(i+1).getTime());
			isNormalized = true;
		}
	}
	
	public boolean isNormalized(){
		return isNormalized;
	}

	public int getNumEntries() {
		return events.size();
	}
}
